/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.meraklisina.latexeditor.mdi.TextEditor;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import javax.swing.text.BadLocationException;
import javax.swing.text.Element ;
import javax.swing.text.Style;

import com.meraklisina.latexeditor.mdi.Pane.SweTextPane;


public class SweExt extends SweTextPane
{
    public int getSelectionStartLine( )
    {
        int start = this.getSelectionStart( ) ;

        Element root = this.getDocument( ).getDefaultRootElement( ) ;
        int lineStart = root.getElementIndex( start ) ;
        return lineStart ;
    }

    public int getSelectionEndLine( )
    {
        int end = this.getSelectionEnd( ) ;

        Element root = this.getDocument( ).getDefaultRootElement( ) ;
        int lineEnd = root.getElementIndex( end ) ;
        return lineEnd ;
    }

    public int getBeginningOfLine( int line )
    {
        Element root = this.getDocument( ).getDefaultRootElement( ) ;
        //System.out.println( "getBeginningOfLine " + line ) ;
        Element el = root.getElement( line ) ;
        if( el == null )
            return -1 ;
        int beginningOfLine = el.getStartOffset( ) ;

        return beginningOfLine ;
    }

    public int getEndOfLine( int line )
    {
        Element root = this.getDocument( ).getDefaultRootElement( ) ;
        int endOfLine = root.getElement( line ).getEndOffset( ) ;

        return endOfLine ;
    }

    public boolean isLineComment( int line )
    {
        int beginningOfLine = this.getBeginningOfLine( line ) ;
        if( beginningOfLine == -1 )
            return false ;
        try
        {
            String txtAtLineBegin = this.getDocument( ).getText( beginningOfLine, 2 ) ;
            if( "%%".equals( txtAtLineBegin ) )
                return true ;
        }
        catch( BadLocationException e )
        {
            e.printStackTrace( ) ;
        }
        return false ;
    }

    public void commentLine( int line )
    {
        int beginningOfLine = this.getBeginningOfLine( line ) ;
        try
        {
            String txtAtLineBegin = this.getDocument( ).getText( beginningOfLine, 2 ) ;
            if( "%%".equals( txtAtLineBegin ) )
                this.appendToDoc( beginningOfLine, beginningOfLine + "%%".length( ), "" ) ;
            else
                this.appendToDoc( beginningOfLine, beginningOfLine, "%%" ) ;
        }
        catch( BadLocationException e )
        {
            e.printStackTrace( ) ;
        }
        System.out.println( line + " : " + beginningOfLine ) ;

    }

    public void toggleComment( )
    {
        if( this.getSelectedText( ) == null )
        {
            int caretPosition = this.getCaretPosition( ) ;
            Element root = this.getDocument( ).getDefaultRootElement( ) ;
            int line = root.getElementIndex( caretPosition ) ;
            this.commentLine( line ) ;
        }
        else
        {
            int selectionEndLine = this.getSelectionEndLine( ) ;

            int selectionStartLine = this.getSelectionStartLine( ) ;

            for( int i = selectionEndLine; i >= selectionStartLine; i-- )
                this.commentLine( i ) ;

        }
    }

    public void appendToDoc( int start, int end, String newText, Style style )
    {
        try
        {
            this.getDocument( ).insertString( start, newText, style ) ;
        }
        catch( BadLocationException e )
        {
            e.printStackTrace( ) ;
        }
    }

    public void appendToDoc( int start, int end, String newText )
    {
        String docText = this.getText( ) ;
        String resText = docText.substring( 0, start ) + newText + docText.substring( end ) ;
        this.setText( resText ) ;
    }

    public void cutTextFromDoc( int start, int end, String newText )
    {
        String docText = this.getText( ) ;
        String resText = docText.substring( 0, start ) + newText + docText.substring( end ) ;
        this.setText( resText ) ;
    }

}

